#encoding: utf-8 
require 'rubygems'
require 'nokogiri'
require 'open-uri'

namespace :db do
  namespace :tv do
    task :load => :environment do
      load_tv
    end

    task :reset => :environment do
      Category.delete_all :kind => 'tv'
      Video.delete_all :kind => 'tv'
    end

    def load_tv      
      tv1_category = Category.find_or_create_by_name_and_kind_and_index("央视台", "tv", 1)
      tv2_category = Category.find_or_create_by_name_and_kind_and_index("卫视台", "tv", 2)
      tv3_category = Category.find_or_create_by_name_and_kind_and_index("其他台", "tv", 3)
      puts system("cd #{Rails.root.to_s} & mkdir -p tmp & mkdir -p tmp/assets")
      30.times.each do |i|
        begin
          url = "http://all.wasu.cn/index/cid/37?&p=#{30-i}"
          index_page = Nokogiri::HTML(open(url))
          index_page.css('#publish dl').each do |dl|

            video_name = dl.at('dd').css('p')[1].css('a')[1].content

            if video_name.include? "CCTV"
              category = tv1_category
              index = 1
            elsif video_name.include? "卫视"
              category = tv2_category
              index = 2
            else
              index =3
              category = tv3_category
            end
            
            video_url = dl.at('dd').css('p').first.css('span a').first['href']
            video_page = Nokogiri::HTML(open(video_url))
            logo_url = dl.css('dd').css('p')[1].css('a')[1]['href']
            logo_page = Nokogiri::HTML(open(logo_url))
            image = logo_page.at('.user_info dl dt a img').attributes['src'].value

            video = Video.find_by_origin_url video_url
            if video
              video.update_attributes :published => true, :category => category
              video.generate_cover(image)
              break
            end
            video = Video.find_or_create_by_name_and_kind video_name, "tv"
            
            puts "\n load vide ================================== #{video.name}  \n"

            video_page.css('ul.play_type_case li').reverse.each do |li|
              clip_name = li.css('a').first.content
              clip_image = li.css('a').first.at('img').attributes['original'].value
              puts clip_image.inspect

              clip_url = li.css('a').first['href']
              puts "......#{clip_url}"
              begin
                clip_page = Nokogiri::HTML(open(clip_url))
                if video and image and clip_name
                  video.generate_cover(image)
                  video.update_attributes :link => video_url, :category => category, :kind => "tv", :origin_url => video_url, :published => true
                  clip_link = clip_page.at('a.f_download')['onclick'].split("'")[3].strip
                  clip_name = clip_page.at('.movie_content .movie_l h3 a').content.gsub(/_(\d+)_/, '')
                  puts clip_name
                  clip = Clip.find_or_create_by_name_and_video_id clip_name, video.id
                  clip.generate_cover clip_image
                  clip.update_attributes :link => clip_link, :kind => 'tv', :published => true
                  puts "load clip ==================================  #{clip.name} \n"
                end
              rescue #Timeout::Error
                puts "Timeout::Error: broken link when load clip #{clip_url}"
              end
            end
          end
        rescue #Timeout::Error
          puts "Timeout::Error: broken link "
        end
      end
    end
  end
end
